"""
Case Type   : SQL语法
Case Name   : A模式下guc参数为enable_set_variable_b_format=off时,set模式修改参
              数报错
Create At   : 2023/04/18
Owner       : cr13
Description :
    1.创建兼容A模式数据库
    2.A库中修改参数
    3.清理环境
Expect      :
    1.成功
    2.合理报错
    3.成功
History     :
"""

import os
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SQL(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.commsh = CommonSH('PrimaryDbUser')
        self.database = 'd_datatype_set_0001'
        self.com = Common()
        self.enable_set_variable_b_format = self.com.show_param(
            "enable_set_variable_b_format")
        result = self.commsh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG,
            "enable_set_variable_b_format=off")
        self.assertTrue(result, '执行失败')

    def test_datatype(self):
        text = "----step1:创建兼容A模式数据库 expect:成功----"
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.database};" \
            f"create database {self.database} dbcompatibility = 'A';"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, result, '执行失败' +
                      text)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, result, '执行失败'
                      + text)

        text = "----step2:A库中修改参数 expect:合理报错----"
        self.log.info(text)
        sql_cmd = f"set global max_connections = 6000;"
        result = self.commsh.execut_db_sql(sql_cmd, dbname=self.database)
        self.log.info(result)
        flg = 'ERROR:  SET UserVar/GLOBAL/SESSION is supported only in ' \
              'B-format database, and enable_set_variable_b_format = on.'
        self.assertIn(flg, result, '执行失败' + text)

    def tearDown(self):
        text = '-----step3:清理环境 expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.database};"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, result, '执行失败' +
                      text)

        result = self.commsh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG,
            f"enable_set_variable_b_format="
            f"{self.enable_set_variable_b_format}")
        self.assertTrue(result, '执行失败')
    
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
